"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".

Replace these with more appropriate tests for your application.
"""

from django.test import TestCase
from django.http import QueryDict

from models import Footballer
from infosets import InfoSet

import django_filters

class FootballFilter(django_filters.FilterSet):
	class Meta:
		model = Footballer
		fields = ['club', 'position', 'name', 'description', 'age']

class FootballGroup(InfoSet):
	"""
	Standard footballer InfoSet definition	
	"""
	
	class Meta:
		queryset = Footballer.objects.all()
		group_fields = ['club', 'position']
		item_fields = ['name', 'description', 'age']
		group_ordering = ['position', 'club']
		items_per_page = 10
		filter_set_class = FootballFilter
		
class FootballGroupAuto(InfoSet):
	"""
	Same as standard, but the filter set class is built automatically rather than referred to
	"""
	
	class Meta:
		queryset = Footballer.objects.all()
		group_fields = ['club', 'position']
		item_fields = ['name', 'description', 'age']
		group_ordering = ['position', 'club']
		items_per_page = 10
		model = Footballer
		
class GrouperTest(TestCase):
	#fixtures = ['footballers.json',]
	
	def setUp(self):
		pass
	
	def testMeta(self):
		"""
		Test that the meta class is propogated okay
		"""
		g = FootballGroup()
		self.assertEqual(g._meta.queryset.count(), Footballer.objects.count())
		self.assertEqual(g._meta.item_ordering, g._meta.item_fields)
		self.assertEqual(g._meta.items_per_page, 10)
		self.assertEqual(g.group_field_names, ['club', 'position'])
	
	def testGrouping(self):
		"""
		Test that default grouping and ordering works correctly
		"""
		f1 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Bergkamp', description = 'Dutch', age = '34'); f1.save()
		f2 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Henry', description = 'French', age = '31'); f2.save()
		f3 = Footballer(club = 'Arsenal', position = 'Goalkeeper', name = 'Seaman', description = 'Tache', age = '50'); f3.save()
		
		f4 = Footballer(club = 'Tottenham', position = 'Midfield', name = 'Modric', description = 'Croat', age = '26'); f4.save()
		f5 = Footballer(club = 'Tottenham', position = 'Defender', name = 'King', description = 'Legend', age = '28'); f5.save()
				
		g = FootballGroup()
		self.assertEqual(g.items_on_page, 5)
		self.assertEqual(g.page.number, 1)
		
		self.assertEqual(len(g.groups), 4)
		
		group1 = g.groups[0]
		self.assertEqual(group1.club, f5.club)
		self.assertEqual(group1.position, f5.position)
		self.assertEqual(len(group1.items), 1)
		g1item1 = group1.items[0]
		self.assertEqual(g1item1.name, f5.name)
		self.assertEqual(g1item1.item, f5)
		
		group4 = g.groups[3]
		self.assertEqual(group4.position, f1.position)
		self.assertEqual(len(group4.items), 2)
		g4item1 = group4.items[0]
		self.assertEqual(g4item1.name, f1.name)
	
	def testCustomOrdering(self):
		f1 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Bergkamp', description = 'Dutch', age = '34'); f1.save()
		f2 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Henry', description = 'French', age = '31'); f2.save()
		f3 = Footballer(club = 'Arsenal', position = 'Goalkeeper', name = 'Seaman', description = 'Tache', age = '50'); f3.save()
		
		f4 = Footballer(club = 'Tottenham', position = 'Midfield', name = 'Modric', description = 'Croat', age = '26'); f4.save()
		f5 = Footballer(club = 'Tottenham', position = 'Defender', name = 'King', description = 'Legend', age = '28'); f5.save()
		g2 = FootballGroup(request = QueryDict('o_gt=asc&o_gi=0')) # now ordered by club ascending
		
		group1 = g2.groups[0]
		self.assertEqual(group1.club, f3.club)
		self.assertEqual(len(group1.items), 1)
		self.assertEqual(group1.items[0].item, f3)
		
		g3 = FootballGroup(request = QueryDict('o_gt=asc&o_gi=0&o_it=desc&o_ii=0')) # now ordered by club ascending, name desc
		
		group1 = g3.groups[0]
		self.assertEqual(group1.club, f3.club)
		self.assertEqual(len(group1.items), 1)
		self.assertEqual(group1.items[0].item, f3)
		group2 = g3.groups[1]
		self.assertEqual(len(group2.items), 2)
		self.assertEqual(group2.items[0].item, f2)
		
	def testPageParams(self):
		f1 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Bergkamp', description = 'Dutch', age = '34'); f1.save()
		f2 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Henry', description = 'French', age = '31'); f2.save()
		f3 = Footballer(club = 'Arsenal', position = 'Goalkeeper', name = 'Seaman', description = 'Tache', age = '50'); f3.save()
		
		f4 = Footballer(club = 'Tottenham', position = 'Midfield', name = 'Modric', description = 'Croat', age = '26'); f4.save()
		f5 = Footballer(club = 'Tottenham', position = 'Defender', name = 'King', description = 'Legend', age = '28'); f5.save()
		
		g = FootballGroup()
		self.assertEqual(g.get_params(), QueryDict('page=1'))
		self.assertEqual(g.next_page_params, QueryDict('page=2'))
		
		g = FootballGroup(request = QueryDict('o_gt=asc&o_gi=0'))
		self.assertEqual(g.get_params(), QueryDict('page=1&o_gt=asc&o_gi=0'))
		self.assertEqual(g.previous_page_params, QueryDict('page=0&o_gt=asc&o_gi=0'))
		
		g = FootballGroup(request = QueryDict('o_gt=asc&o_gi=0&o_it=desc&o_ii=0'))
		self.assertEqual(g.get_params(), QueryDict('page=1&o_gt=asc&o_gi=0&o_it=desc&o_ii=0'))
		self.assertEqual(g.last_page_params, QueryDict('page=1&o_gt=asc&o_gi=0&o_it=desc&o_ii=0'))
		
	def testFieldListsToggle(self):
		g = FootballGroup()
		self.assertEqual(len(g.group_fields), 2)
		
		gf = g.group_fields[0] # club
		self.assertEqual(gf.field, 'club')
		self.assertEqual(gf.label, 'Club')
		self.assertEqual(gf.is_sorted, False)
		# Links
		self.assertEqual(gf.toggle_order_link, QueryDict('page=1&o_gt=asc&o_gi=0'))
		self.assertEqual(gf.toggle_order_link, gf.asc_order_link)
		self.assertEqual(gf.desc_order_link, QueryDict('page=1&o_gt=desc&o_gi=0'))
		
		g = FootballGroup(request = QueryDict('o_gt=asc&o_gi=0'))
		gf = g.group_fields[0]
		self.assertEqual(gf.sorted_ascending, True)
		self.assertEqual(gf.toggle_order_link, gf.desc_order_link)
		self.assertEqual(gf.desc_order_link, QueryDict('page=1&o_gt=desc&o_gi=0'))
		
		g = FootballGroup(request = QueryDict('o_gt=desc&o_gi=0'))
		gf = g.group_fields[0]
		self.assertEqual(gf.sorted_descending, True)
		self.assertEqual(gf.toggle_order_link, QueryDict('page=1'))
		
		g = FootballGroup(request = QueryDict('o_gt=desc&o_gi=1'))
		gf = g.group_fields[0]
		self.assertEqual(gf.is_sorted, False)
		self.assertEqual(gf.toggle_order_link, QueryDict('page=1&o_gt=asc&o_gi=0'))
		self.assertEqual(gf.toggle_order_link, gf.asc_order_link)
		
	def testAutoMakeFilterSet(self):
		"""
		Test that a filter set is created automatically if required
		"""
		g = FootballGroup()
		a = FootballGroupAuto()
		
		gf = g._meta.filter_set_class
		af = a._meta.filter_set_class
		
		self.assertTrue(issubclass(gf, django_filters.FilterSet))
		self.assertTrue(issubclass(af, django_filters.FilterSet))
		
		self.assertEqual(gf._meta.fields, af._meta.fields)
		self.assertEqual(gf._meta.model, af._meta.model)
		
		self.assertTrue(isinstance(g.filter_set, django_filters.FilterSet))
		self.assertTrue(isinstance(a.filter_set, django_filters.FilterSet))
	
	def testFiltering(self):
		f1 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Bergkamp', description = 'Dutch', age = '34'); f1.save()
		f2 = Footballer(club = 'Arsenal', position = 'Striker', name = 'Henry', description = 'French', age = '31'); f2.save()
		f3 = Footballer(club = 'Arsenal', position = 'Goalkeeper', name = 'Seaman', description = 'Tache', age = '50'); f3.save()
		
		f4 = Footballer(club = 'Tottenham', position = 'Midfield', name = 'Modric', description = 'Croat', age = '26'); f4.save()
		f5 = Footballer(club = 'Tottenham', position = 'Defender', name = 'King', description = 'Legend', age = '28'); f5.save()
		
		g = FootballGroup(request = QueryDict('filter-club=Tottenham'))
		
		self.assertEqual(g.items_on_page, 2)
		self.assertEqual(len(g.groups), 2)
		
		g = FootballGroup(request = QueryDict('filter-club=Arsenal&o_gt=desc&o_gi=1')) # order by position desc
		
		self.assertEqual(g.items_on_page, 3)
		self.assertEqual(len(g.groups), 2)
		
		self.assertEqual(g.groups[0].club, f1.club)
		self.assertEqual(g.groups[0].position, f1.position)
		
		g = FootballGroup(request = QueryDict('filter-club=Tottenham'))
		self.assertEqual(len(g.group_fields), 2)
		
		gf = g.group_fields[0] # club
		self.assertEqual(gf.toggle_order_link, QueryDict('page=1&o_gt=asc&o_gi=0&filter-club=Tottenham'))
		

class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.failUnlessEqual(1 + 1, 2)

__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.

>>> 1 + 1 == 2
True
"""}

